﻿// project      : soapi-notify 
// purpose      : console and growl notifications of new Stack Exchange questions
// source       : http://soapi.info/tools/notify.aspx
// announcement : http://stackapps.com/questions/820
// website      : http://soapi.info/tools/notify.aspx
// license      : MIT/X11 - Copyright (c) 2010 Sky Sanders - See License.txt
// inspiration  : http://stackapps.com/questions/817/senotify-se-new-question-notification
// 
//  

using System.Diagnostics;
using System.Reflection;
using Growl.DisplayStyle;

namespace Soapi.Notify.Displays.Mini
{
    public class SoapiMiniDisplay : MultiMonitorVisualDisplay
    {
        #region Location enum

        public enum Location
        {
            TopLeft = 1,
            TopRight = 2,
            BottomLeft = 3,
            BottomRight = 4
        }

        #endregion

        public const string SettingDisplaylocation = "DisplayLocation";

        private readonly LayoutManager _bllm = new LayoutManager(LayoutManager.AutoPositionDirection.UpRight, 6, 6);
        private readonly LayoutManager _brlm = new LayoutManager(LayoutManager.AutoPositionDirection.UpLeft, 6, 6);
        private readonly LayoutManager _tllm = new LayoutManager(LayoutManager.AutoPositionDirection.DownRight, 6, 6);
        private readonly LayoutManager _trlm = new LayoutManager(LayoutManager.AutoPositionDirection.DownLeft, 6, 6);

        public SoapiMiniDisplay()
        {
            SettingsPanel = new SoapiMiniSettingsPanel();
        }

        public override string Author
        {
            get { return "Sky Sanders"; }
        }

        public override string Description
        {
            get { return "Displays Stack Exchange notifications"; }
        }

        public override string Name
        {
            get { return "soapi-notify-mini"; }
        }

        public override string Version
        {
            get
            {
                Assembly a = Assembly.GetExecutingAssembly();
                FileVersionInfo f = FileVersionInfo.GetVersionInfo(a.Location);
                return f.FileVersion;
            }
        }

        public override string Website
        {
            get { return "http://soapi.info/tools/notify.aspx"; }
        }

        protected override void HandleNotification(Notification notification, string displayName)
        {
            if (notification.CoalescingID.EndsWith("?update"))
            {
                notification.CoalescingID =
                    notification.CoalescingID.Substring(0, notification.CoalescingID.IndexOf("?update"));
                foreach (SoapiMiniWindow window in ActiveWindows)
                {
                    if (window.CoalescingGroup == notification.CoalescingGroup)
                    {
                        window.UpdateUI(notification);
                        break;
                    }
                }
            }
            else
            {
                var win = new SoapiMiniWindow { Tag = this };
                win.SetNotification(notification);
                win.SetDisplayLocation(GetLocationFromSetting());
                Show(win);
            }
        }

        private Location GetLocationFromSetting()
        {
            Location location = Location.TopRight;
            if (SettingsCollection != null && SettingsCollection.ContainsKey(SettingDisplaylocation))
            {
                try
                {
                    object val = SettingsCollection[SettingDisplaylocation];
                    if (val != null)
                    {
                        location = (Location)val;
                    }
                }
                catch
                {
                }
            }
            return location;
        }

        protected override LayoutManager GetLayoutManager(NotificationWindow win)
        {
            var pw = (SoapiMiniWindow)win;
            switch (pw.DisplayLocation)
            {
                case Location.TopLeft:
                    return _tllm;
                case Location.BottomLeft:
                    return _bllm;
                case Location.TopRight:
                    return _trlm;
                default:
                    return _brlm;
            }
        }
    }
}